﻿using System;
using System.Text;
using System.Data;

using LTJ.Common.Generic;
using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;

namespace LTJ.Data.DataProvider
{
    /// <summary>
    /// 基础数据管理操作
    /// </summary>
    public class BaseDatas
    {
        /// <summary>
        /// 创建基础数据
        /// </summary>
        public static int CreateBaseData(BaseDataInfo binfo)
        {
            return DatabaseProvider.GetInstance().CreateBaseData(binfo);
        }
        /// <summary>
        /// 查询当前数据是否唯一
        /// </summary>
        /// <param name="code">数据代码</param>
        /// <param name="tcode">数据类型</param>
        public static bool ExistBaseData(string code, string tcode)
        {
            return DatabaseProvider.GetInstance().ExistBaseData(code, tcode);
        }

        /// <summary>
        /// 修改基础数据
        /// </summary>
        /// <param name="binfo"></param>
        public static void UpdateBaseData(BaseDataInfo binfo)
        {
            DatabaseProvider.GetInstance().UpdateBaseData(binfo);
        }

        /// <summary>
        /// 基础数据分页
        /// </summary>
        public static DataTable GetBaseDataByPage(int pageindex, int pagesize, string condition, string orderstr)
        {
            return DatabaseProvider.GetInstance().GetBaseDataByPage(pageindex, pagesize, condition, orderstr);
        }

        /// <summary>
        /// 基础数据数量
        /// </summary>
        public static int GetBaseDataCount(string conditions)
        {
            return DatabaseProvider.GetInstance().GetBaseDataCount(conditions);
        }
        public static int GetCompBaseDataCount(string conditions)
        {
            return DatabaseProvider.GetInstance().GetCompBaseDataCount(conditions);
        }
        
        /// <summary>
        /// 根据ID获取基本数据信息实体
        /// </summary>
        public static BaseDataInfo GetBaseDataByID(int bid)
        {
            return LoadSingleBaseDataInfo(DatabaseProvider.GetInstance().GetBaseDataByID(bid));
        }
        /// <summary>
        /// 根据ID获取基本数据信息实体
        /// </summary>
        public static BaseDataInfo GetBaseDataByStr(string strWhere)
        {
            return LoadSingleBaseDataInfo(DatabaseProvider.GetInstance().GetBaseDataByStr(strWhere));
        }
        public static DataTable GetBaseDataListByStr(string strWhere)
        {
            return DatabaseProvider.GetInstance().GetBaseDataListByStr(strWhere);
        }
        
        /// <summary>
        /// 删除基本数据
        /// </summary>
        /// <param name="ids">基本数据id组合</param>
        /// <returns></returns>
        public static int DelBaseData(string ids)
        {
            return DatabaseProvider.GetInstance().DelBaseData(ids);
        }

        public static DataTable GetDestportsMsg(string strWhere)
        {
            return DatabaseProvider.GetInstance().GetDestportsMsg(strWhere);
        }
        public static DataTable GetCompBaseDataListByStr(int pageindex, int pagesize, string condition, string orderstr)
        {
            return DatabaseProvider.GetInstance().GetCompBaseDataListByStr( pageindex,  pagesize,  condition,  orderstr);
        }

        /// <summary>
        /// 返回基础数据查询基本条件
        /// </summary>
        /// <param name="scode">基础数据类型code(必填）</param>
        /// <param name="hxstr">航线集合</param>
        /// <param name="searchcname">检索中文名</param>
        /// <param name="searchename">检索英文名</param>
        /// <param name="datatype">数据查询类型</param>
        public static string GetBaseDataCondation(string scode, string hxstr, string searchcname, string searchename, int datatype)
        {
            return DatabaseProvider.GetInstance().GetBaseDataCondation(scode, hxstr, searchcname, searchename, datatype);
        }

        #region helper
        public static BaseDataInfo LoadSingleBaseDataInfo(IDataReader reader)
        {
            BaseDataInfo _info = null;
            if (reader.Read())
            {
                _info = new BaseDataInfo();
                _info.id = TypeConverter.ObjectToInt(reader["id"], 0);
                _info.code = reader["code"].ToString().Trim();
                _info.tcode = reader["tcode"].ToString().Trim();
                _info.name = reader["name"].ToString().Trim();
                _info.ename = reader["ename"].ToString().Trim();
                _info.expand1 = reader["expand1"].ToString().Trim();
                _info.expand2 = reader["expand2"].ToString().Trim();
                _info.expand3 = reader["expand3"].ToString().Trim();

                _info.seacode = reader["seacode"].ToString();
                _info.seaname = reader["seaname"].ToString();
                _info.seaename = reader["seaename"].ToString();
                _info.countrycode = reader["countrycode"].ToString();
                _info.countryname = reader["countryname"].ToString();
                _info.countryename = reader["countryename"].ToString();
            }
            reader.Close();
            return _info;
        }
        #endregion
    }
}
